import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;

public final class ReflectUtils {
    private static final ConcurrentHashMap<Class<?>, ConcurrentHashMap<String, Field>> allClassFields = new ConcurrentHashMap<>();
    private ReflectUtils(){

    }

    /**
     * 查找属性（包括父类的）
     * @param clz
     * @param name
     * @return 查找不到返回null
     */
    public static Field getFieldWhatever(Class<?> clz, String name) {
        ConcurrentHashMap<String, Field> fields = allClassFields.computeIfAbsent(clz, k->new ConcurrentHashMap<String, Field>());
        return fields.computeIfAbsent(name, k->{
            try {
                return clz.getDeclaredField(name);
            } catch (Exception e) {
                if (clz.getSuperclass() != null) {
                    return getFieldWhatever(clz.getSuperclass(), name);
                }
                return null;
            }
        });
    }
}
